<?php namespace App\Http\Controllers\Admin;

use App\Http\Requests;
use App\Http\Controllers\Controller;

use App\User;
use App\Customer;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Hash;

class UserController extends Controller
{
    //商户管理
    public function getMerchant(Request $request)
    {
        $list = User::where('user_type', 1);
        $name = trim($request->get('name', ''));
        $appends = [];
        if ($name) {
            $list = $list->where('name', 'like', '%' . $name . '%')->orWhere('nick', 'like', '%' . $name . '%');
            $appends['name'] = $name;
        }
        $list = $list->paginate(config('site.admin.pageSize'));
        return view('admin.user.merchant', [
            'list' => $list,
            'appends' => $appends
        ]);
    }

    public function getNewm($id = 0)
    {
        $model = User::findOrNew($id);
        return view('admin.user.newm', [
            'model' => $model
        ]);
    }

    public function postNewm(Request $request, $id = 0)
    {
        $model = User::findOrNew($id);
        $data = $request->all();
        //return $data;
        $rules = [
            "name" => "required|unique:admins,name," . $id,
            "nick" => "required|unique:admins,nick," . $id,
            "password" => "min:6|max:18|confirmed",
            "real_name" => "required",
            "repassword" => "",
            "status" => "required|boolean"
        ];
        if (0 == $id) {
            $rules['password'] = "required|min:6|max:18|confirmed";
        }
        $attributes = [
            "name" => "商户账号",
            "nick" => "商户名称",
            "password" => "商户密码",
            "real_name" => "真实姓名",
            "repassword" => "二次输入",
            "status" => "商户状态"
        ];
        $message = [
            "required" => ":attribute不能为空",
            "unique" => ":attribute已被使用",
            "min" => "密码长度应少于6位",
            "max" => "密码长度应不能大于18位",
            "confirmed" => "俩次输入的密码不相同",
            "boolean" => "请选择正确的状态",
        ];
        $validator = \Validator::make($data, $rules, $message, $attributes);
        if ($validator->fails()) {
            $ret = ['state' => 'fail'];
            $message = $validator->messages();
            $ret['message'] = $message;
            foreach ($attributes as $k => $attr) {
                if ($message->has($k)) {
                    $ret['message'] = $message->first($k);
                    continue;
                }
            }
        } else {
            $data['user_type'] = 1;
            if("" != $data['password']){
                $data['password'] = Hash::make($data['password']);
            } else {
                $data['password'] = $model->password;
            }
            $model->fill($data);
            $model->save();//写入数据库
            if ($model->id) {
                $ret = ['state' => 'success', 'message' => '操作成功'];
            } else
                $ret['message'] = '保存出错';
        }
        return \Response::json($ret);
    }

    public function getDelm($id){
        $ret = ['state' => 'fail', 'message' => '未知错误，请刷新重试'];
        if (User::destroy($id)) {
            $ret = ['state' => 'success', 'message' => '删除成功'];
        }
        return \Response::json($ret);
    }

    //用户管理
    public function getCustomer(Request $request)
    {
        $list = Customer::orderBy('nickname', 'DESC');
        $name = trim($request->get('name', ''));
        $status = trim($request->get('status', ''));
        $appends = [];
        if ($name) {
            $list = $list->where('realname', 'like', '%' . $name . '%')->orWhere('nickname', 'like', '%' . $name . '%');
            $appends['name'] = $name;
        }
        if (isset($status) && $status != "null" && $status != "" && 6 != $status) {
            $list = $list->where('status', $status);
        }
        $appends['status'] = 6;
        $list = $list->paginate(config('site.admin.pageSize'));
        return view('admin.user.customer', [
            'list' => $list,
            'appends' => $appends,
            'status' => [0 => "审核通过", 1 => "正常", 2 => "冻结", 3 => "审核未通过", 4 => "审核中"],
        ]);
    }

    public function getCheckc($id = 0)
    {
        if (0 == $id) {
            abort(404);
        }
        $model = Customer::find($id);
        return view('admin.user.checkc', [
            'model' => $model,
        ]);
    }

    public function postCheckc(Request $request, $id = 0)
    {
        if (0 == $id) {
            abort(404);
        }
        $model = Customer::find($id);
        $model->status = "" == $model->status ? 4 : $request->get('status');
        $model->save();
        if ($model->userid) {
            $ret = ['state' => 'success', 'message' => '操作成功'];
        } else {
            $ret = ['state' => 'fail', 'message' => '保存出错'];
        }
        return \Response::json($ret);
    }

    public function getDetailc($id = 0)
    {
        if (0 == $id) {
            abort(404);
        }
        $model = Customer::find($id);
        return view('admin.user.detailc', [
            'model' => $model,
            'status' => [0 => "审核通过", 1 => "正常", 2 => "冻结", 3 => "审核未通过", 4 => "审核中"],
        ]);
    }
}
